package ClientUI;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import Client.Client;
import ClientController.ListenerForServer;
import Protocol.contact.Contact;
import Protocol.user.BasePersonInfo;

public class ClientGUI {
	private Client               client;
	
    //public Login            loginFrame;
	public LoginFrame            loginFrame;
    public HelpDialog       	  helpDialog;
    public MainFrame             mainFrame;
	
	public MainFrame getMainFrame() {
		return mainFrame;
	}
	public ClientGUI(Client   client) {
		this.client = client;
	}
	public void go() {
		initGui();
	}
	public Client getClient() {
		return client;
	}
	private void initGui() {
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				/*loginFrame = new JFrame("Transparent Window");
			    loginFrame.setUndecorated(true);
			    //frame.setLayout(null);
			   logFrame=  new LogFrame(loginFrame, client.getClientGui());
			   logFrame.go();*/
				
				
				loginFrame = new LoginFrame(client.getClientGui());
				//loginFrame = new Login(client.getClientGui());
				
			}
		});
	}
	public  void doHelp() {
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				/*helpDialog = new HelpDialog(loginFrame);
				loginFrame.setVisible(false);
				//loginFrame.dispose();
				helpDialog.setVisible(true);*/
			}
		});
		
	}
	public  void doLogin() {
		boolean   ret = false;
		ret = client.getClientNet().connectTo(client.getClientData().getServer_basic());
		if(ret)  {
			client.getClientNet().sendMsgTo(client.getClientData().getServer_basic(), loginFrame.getLogin_R());
		}
	}	
	public	void doLogout() {
		
	}
	public  void enterMainFrame() {   //  remains to be done here
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				Dimension   screen = Toolkit.getDefaultToolkit().getScreenSize();
				mainFrame = new MainFrame(client.getClientGui());
				mainFrame.realContactPanel.setMyContact(client.getClientData().getSelf_contact());
				mainFrame.setLocation(screen.width/2-mainFrame.getWidth()/2, screen.height/2-mainFrame.getHeight()/2);
				loginFrame.dispose();
				mainFrame.setVisible(true);
				
				//update all the views
				mainFrame.realMySpacePanel.updateServerFileList(client.getClientData().getFileNameLoadedList()); 
				mainFrame.realContactPanel.updateContact(client.getClientData().getSelf_contact());
				mainFrame.realBbsPanel.updateAllThreadStatus(client.getClientData().getThreadStatusList());
				mainFrame.realMsgPanel.updateAllMsg(client.getClientData().getMsgList());
			}
		});
	} 
	public void  displayLoginError() {
		//loginFrame.displayServerFailure();
	}
}
